import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/DashboardSimple.vue')
  },
  {
    path: '/course-shop',
    name: 'CourseShop',
    component: () => import('@/views/CourseShop.vue')
  },
  {
    path: '/users',
    name: 'Users',
    component: () => import('@/views/UserManagement.vue')
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import('@/views/GoodsManagement.vue')
  },
  {
    path: '/audio',
    name: 'Audio',
    component: () => import('@/views/AudioManagement.vue')
  },
  {
    path: '/video',
    name: 'Video',
    component: () => import('@/views/VideoManagement.vue')
  },
  {
    path: '/column',
    name: 'Column',
    component: () => import('@/views/ColumnManagement.vue')
  },
  {
    path: '/live',
    name: 'Live',
    component: () => import('@/views/LiveManagement.vue')
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import('@/views/TestPage.vue')
  },
  // 营销管理
  {
    path: '/marketing/seckill',
    name: 'SeckillManagement',
    component: () => import('@/views/marketing/SeckillManagement.vue')
  },
  {
    path: '/marketing/group',
    name: 'GroupManagement',
    component: () => import('@/views/marketing/GroupManagement.vue')
  },
  {
    path: '/marketing/full',
    name: 'FullManagement',
    component: () => import('@/views/marketing/FullManagement.vue')
  },
  {
    path: '/marketing/give',
    name: 'GiveManagement',
    component: () => import('@/views/marketing/GiveManagement.vue')
  },
  {
    path: '/marketing/limit',
    name: 'LimitManagement',
    component: () => import('@/views/marketing/LimitManagement.vue')
  },
  {
    path: '/marketing/scene',
    name: 'SceneManagement',
    component: () => import('@/views/marketing/SceneManagement.vue')
  },
  {
    path: '/marketing/shipping',
    name: 'ShippingManagement',
    component: () => import('@/views/marketing/ShippingManagement.vue'),
    meta: { title: '包邮管理' }
  },
  // 业务管理
  {
    path: '/bill',
    name: 'BillManagement',
    component: () => import('@/views/BillManagement.vue')
  },
  {
    path: '/assets',
    name: 'AssetsManagement',
    component: () => import('@/views/AssetsManagement.vue')
  },
  {
    path: '/cashback',
    name: 'CashbackManagement',
    component: () => import('@/views/CashbackManagement.vue')
  },
  // 新增页面
  {
    path: '/photo',
    name: 'PhotoManagement',
    component: () => import('@/views/PhotoManagement.vue')
  },
  {
    path: '/menu',
    name: 'MenuManagement',
    component: () => import('@/views/MenuManagement.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn')
  const token = localStorage.getItem('token')
  const userType = localStorage.getItem('userType')
  
  // 如果访问登录页面，直接放行
  if (to.path === '/login') {
    next()
    return
  }
  
  // 检查是否已登录
  if (!isLoggedIn) {
    // 未登录，重定向到登录页面
    next('/login')
    return
  }
  
  // 管理员需要token，普通用户不需要token但有userInfo即可
  if (userType === 'admin' && !token) {
    next('/login')
    return
  }
  
  if (userType === 'user') {
    const userInfo = localStorage.getItem('userInfo')
    if (!userInfo) {
      next('/login')
      return
    }
  }
  
  // 已登录，放行
  next()
})

export default router